package com.yuexiao.config;

import com.alibaba.fastjson.JSON;
import com.yuexiao.entity.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName:LogoutSuccessHandler
 * @Auther: YUEXIAO
 * @Description: 登出成功跳转控制器
 * @Date: 2022/8/15 16:31
 * @Version: v1.0
 */
@Component
@Slf4j
public class LogoutSuccessHandler implements org.springframework.security.web.authentication.logout.LogoutSuccessHandler {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        //清除session
        httpServletRequest.getSession().invalidate();

        //清除redis中登录数据
        redisUtil.del("loginId");

        httpServletResponse.setContentType("application/json;charset=utf-8");
        httpServletResponse.setStatus(200);
        httpServletResponse.getWriter().write(JSON.toJSONString(ResultInfo.success("退出成功")));
    }
}
